package com.meixiang.adapter.shopping;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.GoodsMenuItemEntity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;
import com.meixiang.view.CheckLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品菜单内容列表
 *
 * @author LZRUI
 */
public class FilterMenuContentListAdapter extends RecyclerView.Adapter<FilterMenuContentListAdapter.ContentViewHolder> {

    private SparseArray<GoodsMenuItemEntity> mDatas;
    private int checkItemPosition = 0;

    /**
     * 设置数据
     *
     * @param datas
     */
    public void setData(SparseArray<GoodsMenuItemEntity> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    private OnItemClick onItemClickListener;

    public void setOnItemClickListener(OnItemClick onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_menu_content, parent, false);
        return new ContentViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ContentViewHolder holder, int position) {
        GoodsMenuItemEntity bean = mDatas.get(position);
        final int index = holder.getLayoutPosition();
        if (bean != null) {
            setCheckStatus(position, holder);
            holder.tvMenuName.setText(bean.menuName);
            holder.tvMenuNum.setText(bean.menuNum);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Tool.isFastDoubleClick()) return;
                    setCheckItem(index);
                    onItemClickListener.onItemClick(v, index);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    private void setCheckStatus(int position, ContentViewHolder viewHolder) {
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.checkLl.setChecked(true);
            } else {
                viewHolder.checkLl.setChecked(false);
            }
        }
    }

    private void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataSetChanged();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_menu_name)
        TextView tvMenuName;
        @Bind(R.id.tv_menu_num)
        TextView tvMenuNum;
        @Bind(R.id.check_ll)
        CheckLinearLayout checkLl;

        public ContentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
